/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.serialization.json;

import com.aduib.boot.common.SPI.SPILoader;
import com.aduib.boot.common.json.JsonParser;
import com.aduib.boot.common.json.TypeCompositeToJson;
import com.aduib.boot.common.log.LOG;
import com.aduib.boot.common.serialization.api.ObjectReader;
import com.aduib.boot.common.serialization.api.SerializationClean;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

/**
 * @description: JsonObjectReader
 * @author: zzh
 * @date: 2021/10/13 14:58
 */
public class JsonObjectReader implements ObjectReader, SerializationClean {
    private final BufferedReader reader;

    private JsonParser jsonParser;

    public JsonObjectReader(InputStream input) {
        this.reader = new BufferedReader(new InputStreamReader(input));
        this.jsonParser= SPILoader.INSTANCE.getAdaptiveSPI(JsonParser.class);
    }

    private String readLine() throws IOException {
        String line = reader.readLine();
        if (line == null || line.trim().length() == 0) {
            throw new EOFException();
        }
        return line;
    }

    @Override
    public <T> T readObject(Class<T> v) throws IOException {
        return jsonParser.read(readLine(),new TypeCompositeToJson(v,null));
    }

    @Override
    public <T> T readObject(Class<T> v, Type type) throws IOException {
        return jsonParser.read(readLine(),new TypeCompositeToJson(v,type));
    }

    @Override
    public boolean readBoolean() throws IOException {
        return readObject(boolean.class);
    }

    @Override
    public byte readByte() throws IOException {
        return readObject(byte.class);
    }

    @Override
    public byte[] readBytes() throws IOException {
        return readLine().getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public short readShort() throws IOException {
        return readObject(short.class);
    }

    @Override
    public int readInt() throws IOException {
        return readObject(int.class);
    }

    @Override
    public long readLong() throws IOException {
        return readObject(long.class);
    }

    @Override
    public float readFloat() throws IOException {
        return readObject(float.class);
    }

    @Override
    public double readDouble() throws IOException {
        return readObject(double.class);
    }

    @Override
    public String readUtfString() throws IOException {
        return readObject(String.class);
    }

    @Override
    public void cleanup() {
        try {
            reader.close();
        } catch (IOException e) {
            LOG.w("when stream closed appear exception");
            e.printStackTrace();
        }
    }
}
